//
//  LRNetworkManager.swift
//  WeiboX
//
//  Created by admin on 03/12/2016.
//  Copyright © 2016 iOS_Objective-C. All rights reserved.
//

import UIKit
import AFNetworking

enum requestType {
	case get
	case post
}

class LRNetworkManager: AFHTTPSessionManager {
	
	typealias callBack = (Any?, Error?) -> ()
	
	// MARK: - 单例全局访问点
	static let sharedManager: LRNetworkManager = {
		let manager = LRNetworkManager()
		manager.responseSerializer.acceptableContentTypes?.insert("text/plain")
		//		manager.responseSerializer.acceptableContentTypes?.insert("text/html")
		
		return manager
	}()
	
	func request(type: requestType, urlStr: String, param: Any?, callBack: @escaping callBack) {
		if type == .get {
			get(urlStr, parameters: param, progress: nil, success: { (_, responseObj) in
				callBack(responseObj, nil)
			}, failure: { (_, error) in
				callBack(nil, error)
			})
		} else {
			post(urlStr, parameters: param, progress: nil, success: { (_, responseObj) in
				callBack(responseObj, nil)
			}, failure: { (_, error) in
				callBack(nil, error)
			})
		}
	}
}
// MARK: - 发微博接口
extension LRNetworkManager {
	// 文字微博
	func postNewStatus(status: String, callBack: @escaping callBack) {
		let urlStr = "https://api.weibo.com/2/statuses/update.json"
		
		let access_token = LRUserAccount.loadUserInfo()?.access_token
		let params: [String: Any] = ["access_token": access_token!,
		                             "status": status,
		                             "visible": 0
		]
		request(type: .post, urlStr: urlStr, param: params, callBack: callBack)
	}
	
	// 图文微博
	func postNewPicStatus(status: String, pic: UIImage, callBack: @escaping callBack) {
		let urlStr = "https://upload.api.weibo.com/2/statuses/upload.json"
		
		let access_token = LRUserAccount.loadUserInfo()?.access_token
		let params: [String: Any] = ["access_token": access_token!,
		                             "status": status,
		                             "visible": 0
		]
		
		let jpegData = UIImageJPEGRepresentation(pic, 0.5)!
//		let pngData = UIImagePNGRepresentation(pic)
		
//		post(urlStr, parameters: params, constructingBodyWith: { (formData) -> Void in
//			formData.appendPart(withFileData: jpegData, name: "jpeg", fileName: "test", mimeType: "image/jpeg")
//		}, progress: nil, success: { (_, responseObj) in
//			callBack(responseObj, nil)
//		}) { (_, error) in
//			callBack(nil, error)
//		}
		
		post(urlStr, parameters: params, constructingBodyWith: { (formData) in
			formData.appendPart(withFileData: jpegData, name: "pic", fileName: "test", mimeType: "image/jpeg")
		}, progress: nil, success: { (_, responseObj) in
			callBack(responseObj, nil)
		}) { (_, error) in
			callBack(nil, error)
		}
	}
}



// MARK: - 加载首页数据
extension LRNetworkManager {
	func requestTimeLineData(maxId: Int64, sinceId: Int64, callBack: @escaping callBack) {
		let urlStr = "https://api.weibo.com/2/statuses/friends_timeline.json"
		
		let access_token = LRUserAccount.loadUserInfo()?.access_token
		let param: [String: Any] = ["access_token": access_token!,
		                            "max_id": maxId,
		                            "since_id": sinceId
		]
		print(maxId, sinceId)
		let url = urlStr + "?access_token=" + access_token!
		print(url)
		
		request(type: .get, urlStr: urlStr, param: param, callBack: callBack)
	}
}

// MARK: - OAuth 登录相关接口
extension LRNetworkManager {
	// MARK: - 请求 access_token
	func requestAccessToken(code: String, callBack: @escaping callBack) {
		let urlStr = "https://api.weibo.com/oauth2/access_token"
		
		let param = ["client_id": appKey,
		             "client_secret": appSecret,
		             "grant_type": "authorization_code",
		             "code": code,
		             "redirect_uri": redirect_uri
		]
		request(type: .post, urlStr: urlStr, param: param, callBack: callBack)
	}
	
	// MARK: - 根据拿到的 access_token 获得用户信息
	func requestUserInfo(accesToken: String, uid: Int64, callBack: @escaping callBack) {
		let urlStr = "https://api.weibo.com/2/users/show.json"
		
		let param: [String: Any] = ["access_token": accesToken,
		                            "uid":uid
		]
		
		request(type: .get, urlStr: urlStr, param: param, callBack: callBack)
	}
}
